/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frct_set_mac_ip_direct.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/17 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_mng.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}


void __frct_set_mac_ip_direct(Conntrack *p_st_ct
        , const char *p_src_mac, const char *p_dst_mac
        , u_short us_proto
        , u_int ui_src, u_short us_sport, u_int ui_dest, u_short us_dport
        , int i_status);

static void __case1(void)
{
    Conntrack *p_st_ct = NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_status = APM_CONNECT_SYN_SENT;
    char *p_tmp = NULL;

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    __frct_set_mac_ip_direct(p_st_ct, p_src_mac, p_dst_mac, us_proto
                                , ui_src, us_sport, ui_dest, us_dport, i_status);

    CU_ASSERT_PTR_NULL(p_st_ct);

    frfree(p_src_mac);
    frfree(p_dst_mac);

    return;

}

static void __case2(void)
{
    Conntrack *p_st_ct = NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_UDP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_status = APM_CONNECT_SYN_SENT;
    char *p_tmp = NULL;

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));

    __frct_set_mac_ip_direct(p_st_ct, p_src_mac, p_dst_mac, us_proto
                                , ui_src, us_sport, ui_dest, us_dport, i_status);

    CU_ASSERT_EQUAL(p_st_ct->ip_proto,IPPROTO_UDP);

    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_src_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_dst_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_EQUAL(p_st_ct->src_port,80);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,22);

    frfree(p_dst_mac);
    frfree(p_src_mac);
    frfree(p_st_ct);
}


static void __case3(void)
{
    Conntrack *p_st_ct = NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_TCP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_status = APM_CONNECT_SYN_RECV;
    char *p_tmp = NULL;

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));

    __frct_set_mac_ip_direct(p_st_ct, p_src_mac, p_dst_mac, us_proto
                                , ui_src, us_sport, ui_dest, us_dport, i_status);

    CU_ASSERT_EQUAL(p_st_ct->ip_proto,IPPROTO_TCP);

    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_dst_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_src_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->src_port,22);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,80);

    frfree(p_dst_mac);
    frfree(p_src_mac);
    frfree(p_st_ct);
}

static void __case4(void)
{
    Conntrack *p_st_ct = NULL;
    char *p_src_mac = NULL;
    char *p_dst_mac = NULL;
    u_short us_proto = IPPROTO_TCP;
    u_int ui_src = iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    u_int ui_dest = iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_dport = 22;
    int i_status = APM_CONNECT_ESTABLISHED;
    char *p_tmp = NULL;

    p_tmp = (char *)frmalloc(strlen("6e:80:00:ea:9f:8c") + 1);
    snprintf(p_tmp, strlen("6e:80:00:ea:9f:8c") + 1,"%s","6e:80:00:ea:9f:8c");
    p_src_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_src_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_tmp = (char *)frmalloc(strlen("00:8e:6c:90:fe:a4") + 1);
    snprintf(p_tmp, strlen("00:8e:6c:90:fe:a4") + 1,"00:8e:6c:90:fe:a4");
    p_dst_mac = (char *)frmalloc(APM_MAC_SIZE);
    iptools_parse_mac(p_tmp,(unsigned char*)p_dst_mac,APM_MAC_SIZE);
    frfree(p_tmp);

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));

    __frct_set_mac_ip_direct(p_st_ct, p_src_mac, p_dst_mac, us_proto
                                , ui_src, us_sport, ui_dest, us_dport, i_status);

    CU_ASSERT_EQUAL(p_st_ct->ip_proto,IPPROTO_TCP);

    CU_ASSERT_STRING_EQUAL(p_st_ct->src_mac,p_src_mac);
    CU_ASSERT_STRING_EQUAL(p_st_ct->dst_mac,p_dst_mac);
    CU_ASSERT_EQUAL(p_st_ct->src_ip,iptools_parse_ipaddr((char *)"192.168.1.1"));
    CU_ASSERT_EQUAL(p_st_ct->dst_ip,iptools_parse_ipaddr((char *)"192.168.0.1"));
    CU_ASSERT_EQUAL(p_st_ct->src_port,80);
    CU_ASSERT_EQUAL(p_st_ct->dst_port,22);

    frfree(p_dst_mac);
    frfree(p_src_mac);
    frfree(p_st_ct);
}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frct_set_mac_ip_direct 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", __case1);

   	CU_add_test(pSuite, "case2", __case2);

   	CU_add_test(pSuite, "case3", __case3);

   	CU_add_test(pSuite, "case4", __case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}